//@author A0101906R
#include "CmdSearch.h"

using namespace Twentyfifth;
using namespace std;

const std::string CmdSearch::FEEDBACK_SEARCHOK = "Display search results.\n";
const std::string CmdSearch::ERROR_UNDOSEARCH = "Search cannot be undone.\n";
const std::string CmdSearch::ERROR_EMPTYSEARCH = "Please enter the keyword for search.\n";
//@author A0101906R
CmdSearch::CmdSearch() {
	cmdType = SEARCH;
}
//@author A0101906R
Status CmdSearch::execution() {

	Status searchStatus;
	assert(storage != NULL);
	try {
		if (!searchDate.isBlankDate()) {
			searchStatus = storage->searchTasks(searchDate);
		} else {
			checkException();
			searchStatus = storage->searchTasks(task.getDescription());
		}
		setDescForSearch(searchStatus);

	}
	catch (string error) {
		searchStatus.desc = error;
	}
	return searchStatus;
}
//@author A0101906R
Status CmdSearch::undo(Status::StatusCode c) {
	return Status(Status::ERR_OTHER, ERROR_UNDOSEARCH);
}
//@author A0101906R
void CmdSearch::setDescForSearch(Status& s) {
	if (s.code == Status::OK) {
		s.desc = FEEDBACK_SEARCHOK;
	} else {
		s.desc = statusCodeToString(s);
	}
	return;
}
//@author A0101906R
void CmdSearch::checkException() {
	string toBeSearched;
	toBeSearched = task.getDescription();
	if (toBeSearched.size() == 0) {
		throw ERROR_EMPTYSEARCH;
	}
	return;
}